//
//  LXEmoticonTools.swift
//  weibo
//
//  Created by lixin on 16/4/25.
//  Copyright © 2016年 lixin. All rights reserved.
//

import UIKit

//最多显示列数
let LXEmoticonMaxCol = 7

//最多显示行数
let LXEmoticonMaxRow = 3

//最多显示表情个数
let LXEmoticonMaxCount = LXEmoticonMaxCol * LXEmoticonMaxRow - 1

class LXEmoticonTools: NSObject {

    //全局访问点
    static let sharedTools:LXEmoticonTools = LXEmoticonTools()
    
    //读取bundle文件
    lazy var emoticonBundle: NSBundle = {
        
        let path = NSBundle.mainBundle().pathForResource("Emoticons.bundle", ofType: nil)!
        
        return NSBundle(path: path)!
    }()
    
    //默认表情
    lazy var defaultEmoticon:[LXEmoticon] = {
        
        return self.getEmoticons("default/")
    }()
    
    //Emoji
    lazy var emojiEmoticon:[LXEmoticon] = {
        
        return self.getEmoticons("emoji/")
    }()
    
    //浪小花
    lazy var lxhEmoticon:[LXEmoticon] = {
        
        return self.getEmoticons("lxh/")
    }()
    
    
    //emoticon三维表情数组
    lazy var allEmoticons: [[[LXEmoticon]]] = {
        
        return [
            
            self.getEmoticonGroup(self.defaultEmoticon),
            
            self.getEmoticonGroup(self.emojiEmoticon),
            
            self.getEmoticonGroup(self.lxhEmoticon)]
    }()
    
    //MARK: - 一维数组截取成二维数组
    func getEmoticonGroup(emoticon: [LXEmoticon]) -> [[LXEmoticon]]{
        
        let pageCount = (emoticon.count + LXEmoticonMaxCount - 1) / LXEmoticonMaxCount
        
        var tempArr: [[LXEmoticon]] = [[LXEmoticon]]()
        
        for index in 0..<pageCount{
           
            let loc = index * LXEmoticonMaxCount
            
            var length = LXEmoticonMaxCount
            //默认表情有108个 会越界
            if loc + length > emoticon.count{
                
                length = emoticon.count - loc
            }
            
            let range = NSMakeRange(loc, length)
            
            let emoticons =  (emoticon as NSArray).subarrayWithRange(range) as! [LXEmoticon]
            
            tempArr.append(emoticons)
        }
        
        return tempArr
    }
    
    
    //MARK: - 获取表情模型一维数组
    func getEmoticons(path: String) -> [LXEmoticon]{
        
        let file = emoticonBundle.pathForResource("\(path)info.plist", ofType: nil)!
        
        let array = NSArray(contentsOfFile: file)!
       
        var tempArr: [LXEmoticon] = [LXEmoticon]()
        
        for dic  in array{
            
            let emoticon = LXEmoticon(dict: dic as! [String: AnyObject])
            
            //全路径
            emoticon.path = path
            
            tempArr.append(emoticon)
        }
        
        return tempArr
    }
    
    //MARK: - 判断chs是否和本地的相同
    func getEmoticonWithChs(chs: String) -> LXEmoticon?{
        
        //判断是否在默认表情中
        for emoticon in defaultEmoticon{
        
            if emoticon.chs == chs {
                
                return emoticon
            }
        }
        
        //判断是否在浪小花中
        for emoticon in lxhEmoticon{
        
            if emoticon.chs == chs {
                
                return emoticon
            }
        }
        
        return nil
    }
    
}
